package com.gz.payment.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pay/api")
public class PaymentController {

    @Value("${server.port}")
    private String port;


    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("test")
    public HashMap<String, String> getMessage() {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("port", port);
        map.put("message", "success");
        return map;
    }

    @GetMapping("test2")
    public HashMap<String, String> getMessage2() {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("port", port);
        map.put("message", "success");
        return map;
    }

    @GetMapping("discovery")
    public Object getDiscoverInfo(){
        List<String> services = discoveryClient.getServices();
        services.forEach(System.out::println);

        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        instances.forEach(e->{
            System.out.println(e.getHost()+","+e.getInstanceId());
        });
        return discoveryClient;
    }
}
